package com.cysnake.quitsmoke.common.dao;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import com.cysnake.quitsmoke.VO.PlanVO;
import com.cysnake.quitsmoke.tools.FormatTools;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class PlanDao extends BaseDao {

	public static final String TABLE_NAME = "plan";
	public static final String KEY_ID = "id";
	public static final String KEY_STARTTIME = "startTime";
	public static final String KEY_ENDTIME = "endTime";
	public static final String KEY_SMOKENUM = "smokeNum";

	private static final String TAG = "PlanDao";
	private DatabaseHelper mDbHelper;
	private SQLiteDatabase mDb;
	private final Context mCtx;

	public PlanDao(Context ctx) {
		this.mCtx = ctx;
	}

	public PlanDao open() throws SQLException {
		mDbHelper = new DatabaseHelper(mCtx);
		mDb = mDbHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		mDbHelper.close();
	}

	public long create(PlanVO plan) {
		ContentValues initialValues = new ContentValues();
		if (plan.getStartTime() != null) {
			initialValues.put(KEY_STARTTIME,
					FormatTools.dateToString(plan.getStartTime()));
		}
		if (plan.getEndTime() != null) {
			initialValues.put(KEY_ENDTIME,
					FormatTools.dateToString(plan.getEndTime()));
		}
		if (plan.getSmokeNum() >= 0) {
			initialValues.put(KEY_SMOKENUM, plan.getSmokeNum());
		}
		initialValues.put(KEY_ID, plan.getId());
		Log.d("test", "PlanDao:create");
		return mDb.insert(TABLE_NAME, null, initialValues);
	}

	public boolean deletePlanById(long rowId) {

		return mDb.delete(TABLE_NAME, KEY_ID + " = " + rowId, null) > 0;
	}

	public ArrayList<PlanVO> getAllPlan() {
		Cursor cursor = mDb.query(TABLE_NAME, new String[] { KEY_ID,
				KEY_STARTTIME, KEY_ENDTIME, KEY_SMOKENUM }, null, null, null,
				null, null);
		return changeCursor(cursor);

	}

	public PlanVO getPlanById(long rowId) throws SQLException {

		Cursor mCursor = mDb.query(true, TABLE_NAME, new String[] { KEY_ID,
				KEY_STARTTIME, KEY_ENDTIME, KEY_SMOKENUM }, KEY_ID + " = "
				+ rowId, null, null, null, null, null);
		List<PlanVO> list = changeCursor(mCursor);
		if (list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}

	}

	public boolean update(PlanVO plan) {
		ContentValues args = new ContentValues();
		if (plan.getStartTime() != null) {
			args.put(KEY_STARTTIME,
					FormatTools.dateToString(plan.getStartTime()));
		}
		if (plan.getEndTime() != null) {
			args.put(KEY_ENDTIME, FormatTools.dateToString(plan.getEndTime()));
		}
		if (plan.getSmokeNum() >= 0) {
			args.put(KEY_SMOKENUM, plan.getSmokeNum());
		}
		Log.d("test", "PlanDao:udpate");
		return mDb.update(TABLE_NAME, args, KEY_ID + "=" + plan.getId(), null) > 0;
	}

	public int deleteAll() {
		int i = mDb.delete(TABLE_NAME, null, null);
		return i;
	}

	private ArrayList<PlanVO> changeCursor(Cursor cursor) {
		ArrayList<PlanVO> list = new ArrayList<PlanVO>();
		while (cursor.moveToNext()) {
			PlanVO plan = new PlanVO();
			int temp = cursor.getColumnIndex(KEY_SMOKENUM);
			plan.setSmokeNum((cursor.getInt(temp)));
			temp = cursor.getColumnIndex(KEY_ID);
			plan.setId(cursor.getInt(temp));
			temp = cursor.getColumnIndex(KEY_STARTTIME);
			try {
				if (cursor.getString(temp) != null) {
					plan.setStartTime(FormatTools.stringToDate(cursor
							.getString(temp)));
				} else {
					plan.setStartTime(null);
				}
				temp = cursor.getColumnIndex(KEY_ENDTIME);
				if (cursor.getString(temp) != null) {
					plan.setEndTime(FormatTools.stringToDate(cursor
							.getString(temp)));
				} else {
					plan.setEndTime(null);
				}

			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			list.add(plan);
		}
		return list;
	}
}
